﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text.RegularExpressions;

public partial class Category_UserControl_uc_Cate_Route : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
        }
    }
    private void InitData()
    {
        ClearTextBox();
        ZD_CATE_ROUTE objRoute = new ZD_CATE_ROUTE();
        objRoute.ROUTENAME = "";
        objRoute.LEADTIME = -1;
        objRoute.ACTIVE = chkActive.Checked;
        DisplayInGrid(objRoute);
    }

    private void ClearTextBox()
    {       
        hdfRouteID.Value = "-1";
        txtRouteName.Text = string.Empty;
        txtLeadTime.Text = string.Empty;
        chkActive.Checked = true;
        btnSave.Text = "Thêm mới";
        lblAlerting.Text = string.Empty;
    }
    protected void DisplayInGrid(ZD_CATE_ROUTE objRoute)
    {
        CateRouteBO ct = new CateRouteBO();
        List<ZD_PRC_ZD_CATE_ROUTE_SEARCHResult> lst = new List<ZD_PRC_ZD_CATE_ROUTE_SEARCHResult>();
        lst = ct.CateRouteSearch(objRoute).ToList();
        grdRouteList.DataSource = lst;
        grdRouteList.DataBind();
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hdfRouteID.Value = "-1";
        lblAlerting.Text = string.Empty;
        LoadGrid();
    }

    protected void btnXoaTrang_Click(object sender, EventArgs e)
    {
        ClearTextBox();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtRouteName.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập tên tuyến!";
            return;
        }


        if (txtLeadTime.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập tên LeadTime!";
            return;
        }
        try
        {
            int.Parse(txtLeadTime.Text.Trim().Replace(",", ""));
        }
        catch (Exception)
        {
            lblAlerting.Text = "Bạn nhập LeadTime không đúng kiểu dữ liệu!";
            return;
        }
        // Thuc hien Insert Update
        ZD_CATE_ROUTE objRoute = new ZD_CATE_ROUTE();
        objRoute.ROUTEID = int.Parse(hdfRouteID.Value);
        objRoute.ROUTENAME = txtRouteName.Text.Trim();
        objRoute.LEADTIME = int.Parse(txtLeadTime.Text.Trim().Replace(",", ""));
        objRoute.ACTIVE = chkActive.Checked;

        CateRouteBO ct = new CateRouteBO();
        if (int.Parse(hdfRouteID.Value) <= 0)
        {
            hdfRouteID.Value = ct.CateRouteInsert(objRoute).ToString();
            if (int.Parse(hdfRouteID.Value) > 0)
            {
                btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Thêm mới tuyến thành công!";
            }
            else
            {
                lblAlerting.Text = "Thêm mới tuyến thất bại, bạn vui lòng thử lại!";
            }
        }
        else
            if (ct.CateRouteUpdate(objRoute))
            {
                //btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Cập nhật tuyến thành công!";
            }
            else
            {
                lblAlerting.Text = "Cập nhật tuyến thất bại, bạn vui lòng thử lại!";
            }
        LoadGrid();

    }
    private void LoadGrid()
    {
        ZD_CATE_ROUTE objRoute = new ZD_CATE_ROUTE();
        objRoute.ROUTENAME = txtRouteName.Text.Trim();
        int leadTime = -1;
        try
        {
            leadTime = int.Parse(txtLeadTime.Text.Trim());
        }
        catch (Exception)
        {

            
        }
        objRoute.LEADTIME = leadTime;
        objRoute.ACTIVE = chkActive.Checked;
        DisplayInGrid(objRoute);
    }


    
    protected void grdRouteList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadGrid();
        grdRouteList.PageIndex = e.NewPageIndex;
        grdRouteList.DataBind();
    }
    protected void grdRouteList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdRouteList_RowEditing(object sender, GridViewEditEventArgs e)
    {

        lblAlerting.Text = string.Empty;
        btnSave.Text = "Cập nhật";
        grdRouteList.EditIndex = e.NewEditIndex;
        hdfRouteID.Value = grdRouteList.DataKeys[e.NewEditIndex].Value.ToString();
        string strRouteName = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingRouteName")).Text;
        string strLeadTime = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingLeadTime")).Text;
        bool Active = bool.Parse(((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingActive")).Text);

        // Bind len control
        txtRouteName.Text = strRouteName;
        txtLeadTime.Text = strLeadTime;        
        chkActive.Checked = Active;
    }

}